<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手风琴菜单</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            border: 0px;
        }
        #nav {
            width: 600px;
            margin: 0px;
            padding: 0px;
            font-size: 14px;
            line-height: 30px;
        }
        #nav li {
            width: 180px;
            padding-left: 20px;
            padding-bottom: 1px;
            list-style-image: none;
            list-style-type: none;
            background-color: #FFFFFF;
        }
        #nav a {
            padding-left: 20px;
            background-color: #BFBFBF;
            display: block;
            text-decoration: none;
        }
        #nav a:hover {
            background-color: #FF9175;
        }
        #nav li ul {
            padding-top: 1px;
            list-style-image: none;
            list-style-type: none;
        }
        #nav li li {
            padding-left: 0px;
        }
        #nav ul a {
            background-color: #EBEBEB;
        }
        .cx {
            display: none;
            visibility: hidden;
        }
        .ex {
            display: inherit;
            visibility: inherit;
        }
    </style>
    <script>
        window.onload = function () {
            statUp();
        }
    </script>
</head>

<body>
    <!-- <form id="form1" runat="server"> -->
        <div id="Parent">
            <ul id="nav">
                <li><a href="javascript:void(0);" onclick="doMenu(this,'1')">菜单1</a>
                    <ul>
                        <li><a href="javascript:void(0);">菜单1_1</a></li>
                        <li><a href="javascript:void(0);">菜单1_2</a></li>
                    </ul>
                </li>
                <li><a href="javascript:void(0);" onclick="doMenu(this,'1')">菜单2</a>
                    <ul>
                        <li><a href="javascript:void(0);" onclick="doMenu(this,'2')">菜单2_1</a>
                            <ul>
                                <li><a href="javascript:void(0);">菜单2_1_1</a></li>
                                <li><a href="javascript:void(0);">菜单2_1_2</a></li>
                            </ul>
                        </li>
                        <li><a href="javascript:void(0);" onclick="doMenu(this,'2')">菜单2_2</a>
                            <ul>
                                <li><a href="javascript:void(0);">菜单2_2_1</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    <!-- </form> -->
</body>
<script>
    function doMenu(obj, strDeep) {
        var items = obj.parentNode.getElementsByTagName("ul");
        //获取a 对象你节点li 下包含的 所有ul集合
        var itmUl;
        var deeps = strDeep; //strDeep 为当前菜单的级数
        if (items.length > 0) {
            itmUl = items[0];
            // alert(itmUl);
        }
        if (itmUl.className != "ex") {
            cxAll();//当前节点为关闭状态时,先执行关闭所有ul子菜单
            if (deeps == '2') { //若要展开三级菜单当,还要将其二级父菜单展开
                itmUl.parentNode.parentNode.className = "ex";
            }
            itmUl.className = "ex"; //展开下级菜单
        } else {
            itmUl.className = "cx";
        }
    }
    function statUp() {
        cxAll();
        var ulDom = document.getElementById("nav");
        var items = ulDom.getElementsByTagName("ul");
    }
    function cxAll() {
        var ulDom = document.getElementById("nav");
        var items = ulDom.getElementsByTagName("ul");
        for (var i = 0; i < items.length; i++) {
            items[i].className = "cx";
        }
    }
</script>

</html>